//
//  ILGTestTableVC.m
//  ILGDemo
//
//  Created by happy on 2020/3/16.
//  Copyright © 2020 Happy. All rights reserved.
//

#import "ILGTestTableVC.h"

static int kCellHeight = 38;

@interface ILGTestTableVC ()
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) CGFloat originalOffsetY;
@end

@implementation ILGTestTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initData];
    
    [self createUI];
        
    [self learnOthers];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if (self.originalOffsetY == 0) {
        self.originalOffsetY = self.tableView.contentOffset.y;
    }
    NSLog(@"self.view.safeAreaInsets = %@", NSStringFromUIEdgeInsets(self.tableView.safeAreaInsets));
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

- (void)initData {
    kCellHeight = 28;
    
    self.dataArr = [@[@"1", @"2", @"3", @"4", @"5", @"6"] mutableCopy];
}


#pragma mark - UI
static NSString *kCellIdentifier = @"cellIdentifier";

- (void)createUI {
    self.navigationItem.title = @"动画中心";
    
    UIBarButtonItem *performBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"执行" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClicked)];
    UIBarButtonItem *resetButonItem = [[UIBarButtonItem alloc] initWithTitle:@"重置" style:UIBarButtonItemStylePlain target:self action:@selector(resetBtnClicked)];
//    resetButonItem.image = [UIImage imageNamed:@"btn_backItem_white"];
    [self.navigationItem setRightBarButtonItems:@[resetButonItem, performBtnItem]];
    
    UITableView *tableView = self.tableView;
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier];
    tableView.tableFooterView = [UIView new]; //去掉底部多余的线条
    tableView.estimatedRowHeight = YES;
    tableView.rowHeight = UITableViewAutomaticDimension;

    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore:)];
//    footer.ignoredScrollViewContentInsetBottom = 50;
    tableView.mj_footer = footer;
    
    NSString *vcName = NSStringFromClass([self class]);
    tableView.tag = vcName.hash;
}


#pragma mark - delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%s", __func__);
    NSLog(@"self.view.safeAreaInsets = %@", NSStringFromCGPoint(self.tableView.contentOffset));

    
    CGFloat offsetY = scrollView.contentOffset.y - self.originalOffsetY;
    if (offsetY >= 44) {
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setBarTintColor:nil];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%s", __func__);
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    cell.textLabel.text = [NSString stringWithFormat:@"我是第%@行", self.dataArr[indexPath.row]];
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"%s", __func__);
//    return kCellHeight;
//}


#pragma mark - event

- (void)rightBtnClicked {
    [self tb_performAnimationOne];
}

- (void)resetBtnClicked {
    [self tb_modifyRowHeight:28];
}

#pragma mark 如何知道动画做完呢？
- (void)tb_performAnimationOne {
    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        NSLog(@"动画执行完毕");
    }];

    [self tb_modifyRowHeight:50];
    
    [CATransaction commit];
}

- (void)tb_insertRows {
    // 指定插入行数
    int insertRow = 10;
    NSMutableArray *tmpArr = [NSMutableArray array];
    for (int i = 0; i < insertRow; i++) {
        NSString *str = [NSString stringWithFormat:@"%d", -i];
        [self.dataArr insertObject:str atIndex:0];
        
        [tmpArr addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }
    
    // 方案一：采用默认动画时长
//    [self.tableView beginUpdates];
//    [self.tableView insertRowsAtIndexPaths:tmpArr withRowAnimation:UITableViewRowAnimationAutomatic];
//    [self.tableView endUpdates];
//
//    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 插入后，滚到顶部
    
    
    // 方案二：指定动画时长
    [UIView animateWithDuration:0.5 animations:^{
        [self.tableView insertRowsAtIndexPaths:tmpArr withRowAnimation:UITableViewRowAnimationAutomatic];
    
    } completion:^(BOOL finished) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    }];
}

- (void)tb_modifyRowHeight:(int)rowHeight {
    kCellHeight = rowHeight;
    
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}


#pragma mark refresh
- (void)loadMore:(MJRefreshFooter *)refreshFooter {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
        
        NSMutableArray *tmpArr = [NSMutableArray array];
        for (int i = self.dataArr.count; i < self.dataArr.count+20; i++) {
            NSString *str = [NSString stringWithFormat:@"%d", i];
            [tmpArr addObject:str];
        }
        
        [self.dataArr addObjectsFromArray:tmpArr];
        [self.tableView reloadData];
    });
}

#pragma mark - others

- (void)learnOthers {
   
}


@end
